from mitmproxy import http, ctx
import mitmproxy.websocket
import json
import time
import re
from traceback import print_exc
from sqlalchemy import Column, create_engine
from sqlalchemy.dialects.mysql import BIGINT, VARCHAR, INTEGER
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

# 创建对象的基类:
Base = declarative_base()


# 定义User对象:
class User(Base):
    # 表的名字:
    __tablename__ = 'user'

    # 表的结构:
    id = Column(BIGINT(12), primary_key=True)
    token = Column(VARCHAR(80))
    cid = Column(VARCHAR(80))
    ctype = Column(VARCHAR(10))
    name = Column(VARCHAR(30))
    mobile = Column(VARCHAR(15))
    unionid = Column(VARCHAR(80))
    wxid = Column(VARCHAR(80))
    state = Column(VARCHAR(30))
    decrypt_sign = Column(VARCHAR(512))
    point_amount = Column(INTEGER(8))


# 初始化数据库连接:
# engine = create_engine('mysql+pymysql://veve_info:ZnWcNh2zirxdtwsX@81.70.151.135:3306/veve_info', convert_unicode=True)
# 创建DBSession类型:
# DBSession = sessionmaker(bind=engine)


class AuthInfo:

    def request(self, flow: http.HTTPFlow) -> None:
        cookies = flow.request.cookies
        print("cookies::::：", cookies)


    def response(self, flow: http.HTTPFlow) -> None:
        if flow.request.url.startswith("https"):
            resp_dic = flow.response.get_text()
            flow.response.cookies
            print('auth-info  ', resp_dic)

    def websocket_message(self, flow: http.HTTPFlow) -> None:
        assert flow.websocket is not None  # make type checker happy
        # get the latest message
        message = flow.websocket.messages[-1]
        print("message:::", message)

        # was the message sent from the client or server?
        if message.from_client:
            ctx.log.info(f"Client sent a message: {message.content!r}")
        else:
            ctx.log.info(f"Server sent a message: {message.content!r}")


